package com.example.kinytx.apr5;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.res.Resources;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import android.widget.TextView.OnEditorActionListener;
import android.view.View.OnTouchListener;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    private EditText mCallsignView;
    private TextView mPasscodeView;
    private Button mCopyButton;
    private Button mResetButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Resources res = getResources();

        mCallsignView = (EditText) findViewById(R.id._callsign);
        mCallsignView.addTextChangedListener(onCallsignChange);
        mCallsignView.setOnEditorActionListener(editorActionListener);
        mCallsignView.setOnTouchListener(txtCallsign_OnTouch);

        mPasscodeView = (TextView) findViewById(R.id._passcode);
        mPasscodeView.setText("");
        mPasscodeView.setOnClickListener(resetButtonOnClick);

        mCopyButton = (Button) findViewById(R.id._btnCopy);
        mCopyButton.setOnClickListener(copyButtonOnClick);

        mResetButton = (Button) findViewById(R.id._btnReset);
        mResetButton.setOnClickListener(resetButtonOnClick);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * 设置结果到密码显示框
     * @param $passcode
     */
    private void setPasscodeTxt(String $passcode) {
        mPasscodeView.setText($passcode);
    }

    /**
     * 复制数据到剪切板
     * @param content
     * @param context
     */
    public void copy(String content, Context context) {
        ClipboardManager cmb = (ClipboardManager) context
                .getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData cpd = ClipData.newPlainText("Label", content.trim());
        cmb.setPrimaryClip(cpd);
        Toast.makeText(context, "Copied to clipboard", Toast.LENGTH_SHORT).show();
    }

    /**
     * 虚拟键盘完成键被按下时触发
     */
    private OnEditorActionListener editorActionListener = new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            setPasscodeTxt(CallsignAPRSCalculate.calculate(mCallsignView.getText().toString()));
            return false;
        }
    };

    /**
     * 文本框输入改变时触发
     */
    private TextWatcher onCallsignChange = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            setPasscodeTxt(CallsignAPRSCalculate.calculate(s.toString()));
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    };

    private OnTouchListener txtCallsign_OnTouch = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_UP:
                    int curX = (int) event.getX();
                    if (curX > v.getWidth() - 38
                            && !TextUtils.isEmpty(mCallsignView.getText())) {
                        mCallsignView.setText("");
                        int cacheInputType = mCallsignView.getInputType();// backup  the input type
                        mCallsignView.setInputType(InputType.TYPE_NULL);// disable soft input
                        mCallsignView.onTouchEvent(event);// call native handler
                        mCallsignView.setInputType(cacheInputType);// restore input  type
                        return true;// consume touch even
                    }
                    break;
            }
            return false;
        }
    };

    private OnClickListener copyButtonOnClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            copy(mPasscodeView.getText().toString(), getBaseContext());
        }
    };

    private OnClickListener resetButtonOnClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            mCallsignView.setText("");
        }
    };
}